#!/usr/bin/env python

# The function of this script is to combine nodes in the same location and generate new node list and element list into a new tcl file 

# ====================== Input tcl files =====================
inputfiles=["D:\\PyProgram\\po.txt","D:\\PyProgram\\MeshEnd.tcl"]

# ====================== Output tcl files ====================
outputfiles="D:\\PyProgram\\MeshCombined.tcl"

# ====================== Input completes =====================
AllNodeList=[]
AllElementList=[]
for obj in inputfiles:
    content=open(obj)
    NodeList=[]
    ElementList=[]
    for line in content:
        splited=line.split()
        if len(splited)>0:
            if splited[0]=='node':
                NodeList.append([int(splited[1]),float(splited[2]),float(splited[3]),float(splited[4])])
            elif splited[0]=='element':
                ElementList.append([splited[1],int(splited[2]),int(splited[3]),int(splited[4]),int(splited[5]),int(splited[6]),int(splited[7])])
    AllNodeList.append(NodeList)
    AllElementList.append(ElementList)
    content.close()

lenAllNodeList=len(AllNodeList)
lenAllElementList=len(AllElementList)

AllNodeCoord=[]
for i in range(0,lenAllNodeList):
    nodecoord=[]
    for obj1 in AllNodeList[i]:
        nodecoord.append([obj1[1],obj1[2],obj1[3]])
    AllNodeCoord.append(nodecoord)

for i in range(1,lenAllNodeList):
    numn=len(AllNodeCoord[i])
    nume=len(AllElementList[i])
    for k in range(0,numn):
        for j in range(0,i):
            if (AllNodeCoord[i][k] in AllNodeCoord[j]):
                newNodeNumindex=AllNodeCoord[j].index(AllNodeCoord[i][k])
                if AllNodeList[j][newNodeNumindex][0]!='canceled':
                    newNodeNum=int(AllNodeList[j][newNodeNumindex][0])
                    print('new',newNodeNum)
                    oldNodeNum=AllNodeList[i][k][0]
                    print('old',oldNodeNum)
                    AllNodeList[i][k][0]='canceled'
                    AllNodeCoord[i][k][0]='canceled'
                    for ii in range(0,nume):
                        for jj in range(2,6):
                            if AllElementList[i][ii][jj]==oldNodeNum:
                                AllElementList[i][ii][jj]=newNodeNum

newf=open(outputfiles,'w')
for i in range(0,lenAllNodeList):
    numn=len(AllNodeCoord[i])
    nume=len(AllElementList[i])
    for k in range(0,numn):
        if AllNodeList[i][k][0]!='canceled':
            newf.write('node %d %f %f %f'%(AllNodeList[i][k][0],AllNodeList[i][k][1],AllNodeList[i][k][2],AllNodeList[i][k][3]))
            newf.write('\n')
            
for i in range(0,lenAllNodeList):
    numn=len(AllNodeCoord[i])
    nume=len(AllElementList[i])        
    for k in range(0,nume):
        newf.write('element %s %d %d %d %d %d %d'%(AllElementList[i][k][0],AllElementList[i][k][1],AllElementList[i][k][2],AllElementList[i][k][3],AllElementList[i][k][4],AllElementList[i][k][5],AllElementList[i][k][6]))
        newf.write('\n')
        
newf.close()


           
